package com.gistmap.ntdc.service;

import com.gistmap.ntdc.exception.ErrorItems;
import com.gistmap.ntdc.exception.ServiceException;
import com.gistmap.ntdc.persistence.domain.Address;
import com.gistmap.ntdc.persistence.domain.Sex;
import com.gistmap.ntdc.persistence.domain.User;
import com.gistmap.ntdc.persistence.repo.AddressRepository;
import com.gistmap.ntdc.util.Constant;
import com.gistmap.ntdc.util.SessionUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangran
 * @date 2018/12/26
 */
@Component
@Transactional
public class AddressService {

    @Autowired
    private AddressRepository addressRepository;
    @Autowired
    private UserService userService;

    public Address find(Long id) {
        return addressRepository.findById(id).orElseThrow(() -> new ServiceException(ErrorItems.ADDRESS_NOT_FIND));
    }

    public Address add(AddressForm form){
        Long uid = SessionUtil.getUser().getId();
        User user = userService.find(uid);
        if (user.getAddresses() != null) {
            if (user.getAddresses().size() == Constant.MAX_OF_ADDRESS) {
                throw new ServiceException(ErrorItems.ADDRESS_LIMIT);
            }
        }
        Address address = new Address();
        return setField(form, address);
    }

    private Address setField(AddressForm form, Address address) {
        address.setAddress(form.address);
        address.setLatitude(form.latitude);
        address.setLongitude(form.longitude);
        address.setName(form.name);
        address.setFloor(form.floor);
        address.setDetail(form.address + form.floor);
        address.setUserName(form.userName);
        address.setSex(form.sex);
        address.setMobile(form.mobile);
        if (form.checked) {
            // 把原来的
            Address defaultAddress = getDefaultAddress();
            defaultAddress.setChecked(false);
            address.setChecked(form.checked);
        }
        return addressRepository.save(address);
    }

    public void delete(Long id) {
        addressRepository.deleteById(id);
    }

    public Address update(AddressForm form) {
        Address address = find(form.id);
        return setField(form, address);
    }

    public List<Address> list() {
        Long id = SessionUtil.getUser().getId();
        User user = userService.find(id);
        return user.getAddresses();
    }

    public Address getDefaultAddress() {
        Long id = SessionUtil.getUser().getId();
        User user = userService.find(id);
        return user.getAddresses().stream()
                .filter(Address::isChecked).collect(Collectors.toList())
                .get(0);
    }


    @Data
    public static class AddressForm{

        private Long id;

        private String longitude;

        private String latitude;

        private String address;

        private String name;

        private String floor;

        private String userName;

        private Sex sex;

        private String mobile;

        private boolean checked;
    }
}
